﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace ClientManager
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            MyDataOp.InitConnectString();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            MyDataOp mdo = new MyDataOp();

            DataSet ds = mdo.CreateDataSet("select * from [有效个人客户$]");
            DataTable dt = DeleteOthers(ds);
            if (ds != null && ds.Tables[0].Rows.Count != 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.Refresh();
            }
        }

        private DataTable DeleteOthers(DataSet ds)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("客户姓名");
            dt.Columns.Add("地址");
            Hashtable ht = new Hashtable();
            int iCount = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["客户姓名"] == "")
                {
                    i = ds.Tables[0].Rows.Count;
                    iCount = i;
                }
                //if (ht[ds.Tables[0].Rows[i]["客户姓名"].ToString()+ds.Tables[0].Rows[i]["地址"].ToString()]==null)
                if (ht[ds.Tables[0].Rows[i]["客户姓名"].ToString()] == null)
                {
                    //ht.Add(ds.Tables[0].Rows[i]["客户姓名"].ToString() + ds.Tables[0].Rows[i]["地址"].ToString(), "");
                    ht.Add(ds.Tables[0].Rows[i]["客户姓名"].ToString(), "");
                    dt.Rows.Add(dt.NewRow());
                    dt.Rows[dt.Rows.Count - 1]["客户姓名"] = ds.Tables[0].Rows[i]["客户姓名"];
                    dt.Rows[dt.Rows.Count - 1]["地址"] = ds.Tables[0].Rows[i]["地址"];

                }
            }

            string strSql = "delete from [有效单位直客$]";
            MyDataOp mdo = new MyDataOp();
            //mdo.ExecuteCommand(strSql);

            return dt;
        }

        //    public static bool SaveDataTableToExcel(System.Data.DataTable excelTable, string filePath) 
        //    { 
        //        Microsoft.Office.Interop.Excel.Application app = 
        //            new Microsoft.Office.Interop.Excel.ApplicationClass(); 
        //        try 
        //        { 
        //            app.Visible = false; 
        //            Workbook wBook = app.Workbooks.Add(true); 
        //            Worksheet wSheet = wBook.Worksheets[1] as Worksheet; 
        //            if (excelTable.Rows.Count > 0) 
        //            { 
        //                int row = 0; 
        //                row = excelTable.Rows.Count; 
        //                int col = excelTable.Columns.Count; 
        //                for (int i = 0; i < row; i++) 
        //                { 
        //                    for (int j = 0; j < col; j++) 
        //                    { 
        //                        string str = excelTable.Rows[i][j].ToString(); 
        //                        wSheet.Cells[i + 2, j + 1] = str; 
        //                    } 
        //                } 
        //            } 

        //            int size = excelTable.Columns.Count; 
        //            for (int i = 0; i < size; i++) 
        //            { 
        //                wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName; 
        //            } 
        //            //设置禁止弹出保存和覆盖的询问提示框 
        //            app.DisplayAlerts = false; 
        //            app.AlertBeforeOverwriting = false; 
        //            //保存工作簿 
        //            wBook.Save(); 
        //            //保存excel文件 
        //            app.Save(filePath); 
        //            app.SaveWorkspace(filePath); 
        //            app.Quit(); 
        //            app = null; 
        //            return true; 
        //        } 
        //        catch (Exception err) 
        //        { 
        //            MessageBox.Show("导出Excel出错！错误原因：" + err.Message, "提示信息", 
        //                MessageBoxButtons.OK, MessageBoxIcon.Information); 
        //            return false; 
        //        } 
        //        finally 
        //        { 
        //        } 
        //    }
        //}
    }
}